/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.model.PayVersionAbility;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class PayVersionAbilityServiceImpl extends AbstractService implements PayVersionAbilityService
{
	private Logger logger;
	
	public PayVersionAbilityServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
		
	@Override
	public PayVersionAbility getPayVersionAbility(int id) 
	{
		String sql = "select * from payversionability where id=" + String.valueOf(id) + "";
		ModelGenerator<PayVersionAbility> dbcpmg = new ModelGenerator<PayVersionAbility>(PayVersionAbility.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<PayVersionAbility> getPayVersionAbilitys()
	{
		String sql = "select * from payversionability order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<PayVersionAbility> dbcpmg = new ModelGenerator<PayVersionAbility>(PayVersionAbility.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean savePayVersionAbility(Account operator, String type, List<PayVersionAbility> list,int payVersionId)
	{
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql[] = new String[list.size()+1];
		sql[0]="delete from payversionability where payVersionId='"+payVersionId+"'";
		PayVersionAbility pa = null;
		for(int i=0;i<list.size();i++)
		{
			pa = list.get(i);
			pa.setId(getTableId("payversionability"));
			sql[i+1]="insert into payversionability(id,payversionid,paytype,paysdkid,createuser,createdate) values " +
					"('"+pa.getId()+"','"+pa.getPayversionid()+"','"+pa.getPaytype()+"','"+pa.getPaysdkid()+"','"+pa.getCreateuser()+"','"+pa.getCreatedate()+"')";
		}
		return ConnectionManager.getInstance().executeBatch(dbcp, sql);
	}
}
